package audi.a4.control.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import audi.a4.control.core.AbstractAudiA4Packet;
import audi.a4.control.core.AudiA4CanCode;

public class CanBusCodeWidget extends JPanel {

	/**
	 * long 
	 */
	private static final long serialVersionUID = 7608716389483914465L;
	private AudiA4CanCode code;
	private int codeNumber = 0;
	private ArrayList<AbstractAudiA4Packet> packets = new ArrayList<AbstractAudiA4Packet>();
	private WindowedGraph graph = null;
	private JCheckBox use = new JCheckBox("Graph");
	private ArrayList <DataChannel> myData =  new ArrayList <DataChannel> ();
	public CanBusCodeWidget(AudiA4CanCode c, int ID) {
		code = c;
		codeNumber = ID;
		setBorder(BorderFactory.createRaisedBevelBorder());

		use.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(use.isSelected()) {
					for(DataChannel d:myData)
						graph.showDataChannel(d);
				}else {
					for(DataChannel d:myData)
						graph.hideDataChannel(d);
				}
			}
		});
		add(use);
		add(new JLabel("Code = "+AudiA4CanCode.getCodeHex(ID)));
		add(new JLabel(code.toString()));
	}

	public int getCanCodeNumber() {
		return codeNumber;
	}
	public void addPacket(AbstractAudiA4Packet p) {
		packets.add(p);
		for(int i=0;i<p.getParsedData().length;i++) {
			DataChannel dc;
			try {
				dc =myData.get(i); 
			}catch(IndexOutOfBoundsException ex) {
				dc = new DataChannel(p.getAudiA4CanCode().toString()+"_"+i);
				myData.add(dc);
			}
			dc.graphValue(p.getParsedData()[i],p.getTimeStamp());
		}
	}

	public void setGraph(WindowedGraph graph) {
		this.graph = graph;
	}

	public WindowedGraph getGraph() {
		return graph;
	}
}
